/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * clientesLista.java
 *
 * Created on 21/10/2010, 01:15:02 AM
 */
package pe.edu.upla.lamerced.desk.view.clientes;

import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pe.edu.upla.lamerced.Inicial;
import pe.edu.upla.lamerced.bean.Users;
import pe.edu.upla.lamerced.service.UsuarioService;

/**
 *
 * @author elvis
 */
public class UsuariosLista extends javax.swing.JInternalFrame {

   private UsuarioService usuarioService;
   private JDesktopPane escritorio;
   private List<Users> listausuarios;

   public JDesktopPane getEscritorio() {
      return escritorio;
   }
   public void setEscritorio(JDesktopPane escritorio) {
      this.escritorio = escritorio;
   }

   /** Creates new form clientesLista */
   public UsuariosLista() {
      initComponents();
   }

   /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
   @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbClientesLista = new javax.swing.JTable();
        btnNuevo = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

        setClosable(true);
        setTitle("Lista de Usuarios del Sistema");

        jScrollPane1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setToolTipText("Lista de Usuarios registrados en el sistema");

        tbClientesLista.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null}
            },
            new String [] {
                "Nombre", "Paterno", "Materno", "Usuario", "Documento"
            }
        ));
        jScrollPane1.setViewportView(tbClientesLista);

        btnNuevo.setText("Nuevo");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 3, 14));
        jLabel4.setText("Lista de Usuarios");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(77, Short.MAX_VALUE)
                .addComponent(btnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43)
                .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(56, 56, 56)
                .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(100, 100, 100))
            .addGroup(layout.createSequentialGroup()
                .addGap(174, 174, 174)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(197, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
       if (tbClientesLista.getSelectedRow()==-1){
          JOptionPane.showMessageDialog(this, "No ha seleccionado ningun Usuario para modificar");
         return;
       }
       UsuariosDatos form = new UsuariosDatos();
       form.setUsuario(listausuarios.get(tbClientesLista.getSelectedRow()));
       form.setEscritorio(escritorio);
       form.getTxtusername().setEnabled(false);
       this.escritorio.add(form);
       form.cargainicial();
       form.setVisible(true);
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        if (tbClientesLista.getSelectedRow()==-1){
         JOptionPane.showMessageDialog(this, "No ha seleccionado ningun usuario");
         return;
       }
       int res = JOptionPane.showConfirmDialog(this,"Seguro que desea eliminar al usuario?","Eliminar Cliente",JOptionPane.YES_NO_OPTION);
       if(res==0){
         Users dt = listausuarios.get(tbClientesLista.getSelectedRow());
         dt.setEnabled(0);
         usuarioService.modificarUsuario(dt);
         JOptionPane.showMessageDialog(this, "se ha eliminado al usuario seleccionado");
         listausuarios = usuarioService.listaUsuarios();
         pintagrilla();
       }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
       UsuariosDatos form = new UsuariosDatos();
       form.setUsuario(new Users());
       this.escritorio.add(form);
       form.cargainicial();
       form.setEscritorio(escritorio);
       form.setVisible(true);
       this.dispose();
    }//GEN-LAST:event_btnNuevoActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbClientesLista;
    // End of variables declaration//GEN-END:variables

   public void cargaInicial(){
      if(usuarioService==null){
        usuarioService = (UsuarioService) Inicial.getFactory().getBean("usuarioService");
      }
      if(listausuarios==null){
         listausuarios = usuarioService.listaUsuarios();
      }
      pintagrilla();
   }
    public void pintagrilla(){
        DefaultTableModel tabla = (DefaultTableModel) tbClientesLista.getModel();
        tabla.setRowCount(0);
        for (Users examen : listausuarios) {
            Object[] fila = new Object[5];
            fila[0] = examen.getNombre();
            fila[1] = examen.getApePaterno();
            fila[2] = examen.getApeMaterno();
            fila[3] = examen.getUsername();
            fila[4] = examen.getNroDocumento();
            tabla.addRow(fila);
        }
    }

}
